﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAO
{
    public class DonVi_DAO
    {
        
        public static DataTable GetDonViByLoginName(string userName, SqlConnection ConPQ)
        {           
            DataTable dt = new DataTable();

            string sp = " SELECT "
                        +"	 MaDV"
                        +"	 ,TenDonVi "
                        +" FROM "
                        + "	PhanQuyen.dbo.fn_NguoiDung_DonVi('" + userName + "')";

            dt = Uit.it_SQL.getDataTable(sp, ConPQ);

            return dt;           
        }

        public static DataTable GetDonViAll( SqlConnection ConPQ)
        {
            DataTable dt = new DataTable();

            string sp = "SELECT                        \n"
                        + "	dv.MaDonVi                  \n"
                        + "	,dv.TenDonVi                \n"
                        + "	,dv.IDLoaiDonVi             \n"
                        + "FROM                          \n"
                        + "	his.dbo.utb_DM_DonVi AS dv  \n"
                        + "WHERE                         \n"
                        + "	dv.NgungSD = 0              \n"
                        + "ORDER BY                      \n"
                        + "	dv.TenDonVi                 \n";

            dt = Uit.it_SQL.getDataTable(sp, ConPQ);

            return dt;
        }

        public static DataTable GetDonViDaAdd(int loaiDV)
        {
            DataTable dt = new DataTable();

            string sp = "SELECT [MaDonVi]                     \n"
                        + "      ,[TenDonVi]                  \n"
                        + "      ," + loaiDV + "AS [LoaiDV]   \n"
                        + "      ,[NgungSD]                   \n"
                        + "FROM                               \n"
                        + "	[SANNHI].[dbo].[GPB_DVGUIMAU] dv \n"
                        + "WHERE                              \n"
                        + "	dv.LoaiDV = " + loaiDV.ToString() + " \n";

            dt = Uit.it_SQL.getDataTable(sp);

            return dt;
        }
        public static Boolean delDMThuocKhoa(string MaDonVi)
        {
            Boolean rs = false;
            string sql = "DELETE FROM [dbo].[GPB_DVGUIMAU] \n"
                    + "      WHERE MaDonVi = '" + MaDonVi + "'";
            rs = Uit.it_SQL.ExecuteNonQuery(sql);
            return rs;
        }

        public static Boolean delAllDMThuocKhoa(int LoaiDV)
        {
            Boolean rs = false;
            string sql = "DELETE FROM [dbo].[GPB_DVGUIMAU] WHERE LoaiDV =" + LoaiDV + " \n";
            rs = Uit.it_SQL.ExecuteNonQuery(sql);
            return rs;
        }

        public static Boolean insDMThuocKhoa(string MaDonVi, String TenDV
                                            , int LoaiDV, bool Xoa)
        {
            Boolean rs = false;
            string sql = "INSERT INTO [dbo].[GPB_DVGUIMAU] \n"
                       + "           ([MaDonVi] \n"
                       + "           ,[TenDonVi] \n"
                       + "           ,[LoaiDV] \n"
                       + "           ,[NgungSD]) \n"
                       + "     VALUES \n"
                       + "           ('"+MaDonVi+"' \n"
                       + "           ,N'"+TenDV  +"' \n"
                       + "           , "+LoaiDV.ToString()+"\n"
                       + "           , '"+Xoa.ToString()   +"')";
            rs = Uit.it_SQL.ExecuteNonQuery(sql);
            return rs;
        }
    }
}
